package com.emapp.lccode.activity

import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import android.widget.FrameLayout
import com.emapp.lccode.R
import com.emapp.lccode.base.BaseActivity1
import com.emapp.lccode.base.EventBusConfig
import com.emapp.lccode.base.EventBusModel
import com.emapp.lccode.base.X5WebView
import com.emapp.lccode.databinding.ActivityWebx5Binding
import com.emapp.lccode.utils.setOnclickNoRepeat
import com.gyf.immersionbar.ktx.immersionBar
import com.tencent.smtt.export.external.TbsCoreSettings.TBS_SETTINGS_USE_DEXLOADER_SERVICE
import com.tencent.smtt.export.external.TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER
import com.tencent.smtt.sdk.QbSdk
import com.tencent.smtt.sdk.WebSettings
import com.tencent.smtt.sdk.WebView
import com.tencent.smtt.sdk.WebViewClient
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


class X5WebActivity : BaseActivity1<BaseViewModel, ActivityWebx5Binding>() {
    private var titlename: String? = null
    private var path: String? = null
    private val mWebView: X5WebView by lazy { X5WebView(this, null) }
    override fun initView(savedInstanceState: Bundle?) {
        immersionBar {
            titleBar(mViewBind.lvTitle.ivBar)
            statusBarDarkFont(true)
            fullScreen(true)
            statusBarColor(R.color.transparent)
            navigationBarColor(R.color.white)
        }
        titlename = intent.getStringExtra("name")
        path = intent.getStringExtra("path")
        mViewBind.lvTitle.tvTitle.text = titlename
        mViewBind.lvTitle.tvRight.visibility = View.GONE
        // 在调用TBS初始化、创建WebView之前进行如下配置
        val map = HashMap<String, Any>()
        map[TBS_SETTINGS_USE_SPEEDY_CLASSLOADER] = true
        map[TBS_SETTINGS_USE_DEXLOADER_SERVICE] = true
        QbSdk.initTbsSettings(map)
        // mWebView = X5WebView(this, null)
        mViewBind.webView1.addView(
            mWebView, FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT
            )
        )
        val webSetting = mWebView!!.settings
        webSetting.allowFileAccess = true
        webSetting.layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS
        webSetting.setSupportZoom(false)
        webSetting.builtInZoomControls = false
        webSetting.useWideViewPort = true //将图片调整到适合webview的大小

        webSetting.setSupportMultipleWindows(false)
        webSetting.setAppCacheEnabled(true)
        webSetting.domStorageEnabled = true
        webSetting.loadWithOverviewMode = true // 缩放至屏幕的大小
        webSetting.pluginState = WebSettings.PluginState.ON_DEMAND
        webSetting.displayZoomControls = false //隐藏webview缩放按钮
        webSetting.javaScriptEnabled = true //支持js
        mWebView.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
                // 得到 URL 可以传给应用中的某个 WebView 页面加载显示
                return false
            }
        }
        mWebView.loadUrl(path)
    }

    override fun onBindViewClick() {
        setOnclickNoRepeat(
            mViewBind.lvTitle.ivLeft, mViewBind.lvTitle.tvRight
        ) {
            when (it.id) {
                R.id.iv_left -> {
                    finish()
                }
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventMainThread(event: EventBusModel) {
        when (event.type) {
            EventBusConfig.FINISH -> {
            }

            else -> {}
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        return if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (mWebView != null && mWebView.canGoBack()) {
                mWebView.goBack()
                true
            } else super.onKeyDown(keyCode, event)
        } else super.onKeyDown(keyCode, event)
    }

    override fun onDestroy() {
        super.onDestroy()
        if (mWebView != null) mWebView.destroy()
    }
}